package com.xijia.wuyouwallet.fragment;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.activity.AttestationActivity;
import com.xijia.wuyouwallet.activity.CommonWvActivity;
import com.xijia.wuyouwallet.activity.UpgradeNewActivity;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseFragment;
import com.xijia.wuyouwallet.bean.SystemData;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class WalletFragment extends BaseFragment implements View.OnClickListener{



    private LinearLayout wallet_pufaCard,wallet_minshengCard,wallet_xingyeCard,wallet_jiaotongCard;
    //新修改
    private LinearLayout wallet_insideCard,wallet_loan,wallet_mouth,wallet_cultivateCard;

    private AlertDialog operationDialog;

    private int can_see_member;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wallet,null);
        initView(view);
        return view;
    }
    private void initView(View view){
        wallet_insideCard = (LinearLayout) view.findViewById(R.id.wallet_insideCard);
        wallet_insideCard.setOnClickListener(this);
        wallet_loan = (LinearLayout) view.findViewById(R.id.wallet_loan);
        wallet_loan.setOnClickListener(this);
        wallet_mouth = (LinearLayout) view.findViewById(R.id.wallet_mouth);
        wallet_mouth.setOnClickListener(this);
        wallet_cultivateCard = (LinearLayout) view.findViewById(R.id.wallet_cultivateCard);
        wallet_cultivateCard.setOnClickListener(this);
        wallet_pufaCard = (LinearLayout) view.findViewById(R.id.wallet_pufaCard);
        wallet_pufaCard.setOnClickListener(this);
        wallet_minshengCard = (LinearLayout) view.findViewById(R.id.wallet_minshengCard);
        wallet_minshengCard.setOnClickListener(this);
        wallet_xingyeCard = (LinearLayout) view.findViewById(R.id.wallet_xingyeCard);
        wallet_xingyeCard.setOnClickListener(this);
        wallet_jiaotongCard = (LinearLayout) view.findViewById(R.id.wallet_jiaotongCard);
        wallet_jiaotongCard.setOnClickListener(this);

        can_see_member = Integer.parseInt(MyApp.systemData.getData().getList().getCan_see_member());
        //Log.e("info","can_see_member="+can_see_member);
        //requestSysConfig();
    }
    /**
     * 获取系统配置
     */
    private void requestSysConfig(){
        Map<String,Object> map = new HashMap<>();
        map.put("user",MyApp.userData.getData().getUser_id());
        map.put("token",MyApp.userData.getData().getToken());
        showProgressDialog(false);
        XUtil.Post(UrlConfig.GET_SYSTEM_CONFIG,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                //MyLogger.wLog().e("获取系统配置result="+result);
                SystemData systemData = JSONObject.parseObject(result,SystemData.class);
                if (systemData.getCode()==200){
                    can_see_member = Integer.parseInt(systemData.getData().getList().getCan_see_member());
                }else {
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                //MyLogger.wLog().e("获取系统配置onError="+ex.toString());
            }
        });
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    public void onResume() {
        super.onResume();
    }
    private void initOperationDialog(final int hitType) {
        operationDialog=new AlertDialog.Builder(getActivity()).create();
        //点击外部区域不能取消dialog
        operationDialog.setCanceledOnTouchOutside(false);
        //dialog.setOnKeyListener(keylistener);
        operationDialog.show();

        Window window = operationDialog.getWindow();
        window.setContentView(R.layout.dialog_operation);
        window.getDecorView().setBackgroundColor(Color.TRANSPARENT);


        //View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_operation, null);
        TextView dialog_binding_sure = (TextView) window.findViewById(R.id.dialog_binding_sure);
        TextView dialog_binding_cancle = (TextView) window.findViewById(R.id.dialog_binding_cancle);
        //TextView dialog_binding_title = (TextView) view.findViewById(R.id.dialog_binding_title);
        TextView dialog_binding_content = (TextView) window.findViewById(R.id.dialog_binding_content);
        if (hitType==1){
            dialog_binding_content.setText("您即将进入的模块为会员收费模块，需付费开通，如需开通请点击立即前往");
        }else if (hitType==2){
            dialog_binding_content.setText("您尚未实名认证");
        }
        dialog_binding_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operationDialog.dismiss();
            }
        });
        dialog_binding_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operationDialog.dismiss();
                if (hitType==1){
                    jumpToPage(UpgradeNewActivity.class);
                }else if (hitType==2){
                    jumpToPage(AttestationActivity.class);
                }
            }
        });
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);
    }

    @Override
    public void onClick(View view) {
        Bundle bundle = null;
        switch (view.getId()){
            case R.id.wallet_insideCard://内部办卡
                if (!MyApp.userData.getData().getRole_id().equals("1")){
                    bundle = new Bundle();
                    bundle.putString("url", UrlConfig.INSIDE_CARD);
                    jumpToPage(CommonWvActivity.class,bundle);
                }else{
                    if (can_see_member==1){
                        bundle = new Bundle();
                        bundle.putString("url", UrlConfig.INSIDE_CARD);
                        jumpToPage(CommonWvActivity.class,bundle);
                    }else if (can_see_member==0){
                        initOperationDialog(1);
                        return;
                    }else {
                        showToast("维护中");
                    }
                }
                break;
            case R.id.wallet_loan://贷款通道
                if (!MyApp.userData.getData().getRole_id().equals("1")){
                    bundle = new Bundle();
                    bundle.putString("url", UrlConfig.LOAN_PASSAGE);
                    jumpToPage(CommonWvActivity.class,bundle);
                }else{
                    if (can_see_member==1){
                        bundle = new Bundle();
                        bundle.putString("url", UrlConfig.LOAN_PASSAGE);
                        jumpToPage(CommonWvActivity.class,bundle);
                    }else if (can_see_member==0){
                        initOperationDialog(1);
                        return;
                    }else {
                        showToast("维护中");
                    }
                }
                break;
            case R.id.wallet_mouth://最新口子
                if (!MyApp.userData.getData().getRole_id().equals("1")){
                    bundle = new Bundle();
                    bundle.putString("url", UrlConfig.NEWCARD_URL);
                    jumpToPage(CommonWvActivity.class,bundle);
                }else{
                    if (can_see_member==1){
                        bundle = new Bundle();
                        bundle.putString("url", UrlConfig.NEWCARD_URL);
                        jumpToPage(CommonWvActivity.class,bundle);
                    }else if (can_see_member==0){
                        initOperationDialog(1);
                        return;
                    }else {
                        showToast("维护中");
                    }
                }
                break;
            case R.id.wallet_cultivateCard://养卡秘籍
                if (!MyApp.userData.getData().getRole_id().equals("1")){
                    bundle = new Bundle();
                    bundle.putString("url", UrlConfig.KEEPCARD_URL);
                    jumpToPage(CommonWvActivity.class,bundle);
                }else{
                    if (can_see_member==1){
                        bundle = new Bundle();
                        bundle.putString("url", UrlConfig.KEEPCARD_URL);
                        jumpToPage(CommonWvActivity.class,bundle);
                        return;
                    }else if (can_see_member==0){
                        initOperationDialog(1);
                        return;
                    }else {
                        showToast("维护中");
                        return;
                    }
                }
                break;
            case R.id.wallet_pufaCard://蒲发
                if (!MyApp.userData.getData().getRole_id().equals("1")){
                    bundle = new Bundle();
                    bundle.putString("url", "http://xyk.payqianyan.com/index/index?id=2387&card=pf");
                    jumpToPage(CommonWvActivity.class,bundle);
                }else{
                    if (can_see_member==1){
                        bundle = new Bundle();
                        bundle.putString("url", "http://xyk.payqianyan.com/index/index?id=2387&card=pf");
                        jumpToPage(CommonWvActivity.class,bundle);
                        return;
                    }else if (can_see_member==0){
                        initOperationDialog(1);
                        return;
                    }else {
                        showToast("维护中");
                        return;
                    }
                }
                break;
            case R.id.wallet_minshengCard://民生
                if (!MyApp.userData.getData().getRole_id().equals("1")){
                    bundle = new Bundle();
                    bundle.putString("url", "http://xyk.payqianyan.com/index/index?id=2387&card=ms");
                    jumpToPage(CommonWvActivity.class,bundle);
                }else{
                    if (can_see_member==1){
                        bundle = new Bundle();
                        bundle.putString("url", "http://xyk.payqianyan.com/index/index?id=2387&card=ms");
                        jumpToPage(CommonWvActivity.class,bundle);
                        return;
                    }else if (can_see_member==0){
                        initOperationDialog(1);
                        return;
                    }else {
                        showToast("维护中");
                        return;
                    }
                }
                break;
            case R.id.wallet_xingyeCard://兴业
                if (!MyApp.userData.getData().getRole_id().equals("1")){
                    bundle = new Bundle();
                    bundle.putString("url", "http://xyk.payqianyan.com/index/index?id=2387&card=xy");
                    jumpToPage(CommonWvActivity.class,bundle);
                }else{
                    if (can_see_member==1){
                        bundle = new Bundle();
                        bundle.putString("url", "http://xyk.payqianyan.com/index/index?id=2387&card=xy");
                        jumpToPage(CommonWvActivity.class,bundle);
                        return;
                    }else if (can_see_member==0){
                        initOperationDialog(1);
                        return;
                    }else {
                        showToast("维护中");
                        return;
                    }
                }
                break;
            case R.id.wallet_jiaotongCard://交通
                if (!MyApp.userData.getData().getRole_id().equals("1")){
                    bundle = new Bundle();
                    bundle.putString("url", "http://xyk.payqianyan.com/index/index?id=2387&card=jt");
                    jumpToPage(CommonWvActivity.class,bundle);
                }else{
                    if (can_see_member==1){
                        bundle = new Bundle();
                        bundle.putString("url", "http://xyk.payqianyan.com/index/index?id=2387&card=jt");
                        jumpToPage(CommonWvActivity.class,bundle);
                        return;
                    }else if (can_see_member==0){
                        initOperationDialog(1);
                        return;
                    }else {
                        showToast("维护中");
                        return;
                    }
                }
                break;
        }
    }

}
